package us.sunrisemorning.mykancolle.kcsapi;

import java.lang.reflect.InvocationTargetException;

import com.jfinal.kit.LogKit;

import us.sunrisemorning.mykancolle.mapdata.BaseMapData;
import us.sunrisemorning.mykancolle.model.User;
import us.sunrisemorning.mykancolle.utils.ApiController;

public class ApiReqMapController extends ApiController {
    public void start() {
        User u = getCurrentUser();

        int maparea_id = getParaToInt("api_maparea_id");
        int mapinfo_no = getParaToInt("api_mapinfo_no");
        int deck_id = getParaToInt("api_deck_id");

        try {
            Class<BaseMapData> mapDataClass = (Class<BaseMapData>) BaseMapData.class.forName("us.sunrisemorning.mykancolle.mapdata.MapData" + maparea_id + mapinfo_no);
            BaseMapData mapdata = mapDataClass.getConstructor(Long.TYPE).newInstance(u.getId());

            setSessionAttr(ApiReqSortieController.BATTLE_DECK_ID, deck_id);
			setSessionAttr(ApiReqSortieController.BATTLE_MAP_DATA, mapdata);

			mapdata.moveNext();
			renderApiJson(mapdata.getCurrentCellInfo());
		} catch (ClassNotFoundException e) {
			LogKit.error("找不到对应的地图数据", e);
		} catch (NoSuchMethodException | SecurityException e) {
			LogKit.error("找不到地图数据的构造方法", e);
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException e) {
			LogKit.error("实例化地图数据失败", e);
		}
    }
}
